<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <el-tabs v-model="message">
        <el-tab-pane :label="`未读消息(${unread.length ? unread.length : 0})`" name="first">
          <el-table class="width100" :data="unread" :show-header="false" :empty-text="$t('common.no_data')">
            <el-table-column>
              <template v-slot="scope">
                <el-tag v-if="1 == scope.row.message.type.value">
                  {{ scope.row.message.type.text }}
                </el-tag>
                <el-tag type="warning" v-else-if="2 == scope.row.message.type.value">
                  {{ scope.row.message.type.text }}
                </el-tag>
                <el-tag type="success" v-else-if="3 == scope.row.message.type.value">
                  {{ scope.row.message.type.text }}
                </el-tag>
                <el-tag type="danger" v-else>
                  {{ scope.row.message.type.text }}
                </el-tag>
                <span class="message-title">{{ scope.row.message.title }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="date" width="180"></el-table-column>
            <el-table-column width="120">
              <template v-slot="scope">
                <el-button size="small" @click="handleRead(scope.row.id)">标为已读</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="handle-row">
            <el-button type="primary" @click="handleRead(0)">全部标为已读</el-button>
          </div>
        </el-tab-pane>

        <el-tab-pane :label="`已读消息(${readed.length ? readed.length : 0})`" name="second">
          <template v-if="message === 'second'">
            <el-table
              :data="readed"
              :show-header="false"
              style="width: 100%"
              :empty-text="$t('common.no_data')">
              <el-table-column>
                <template v-slot="scope">
                  <el-tag v-if="1 == scope.row.message.type.value">
                    {{ scope.row.message.type.text }}
                  </el-tag>
                  <el-tag type="warning" v-else-if="2 == scope.row.message.type.value">
                    {{ scope.row.message.type.text }}
                  </el-tag>
                  <el-tag type="success" v-else-if="3 == scope.row.message.type.value">
                    {{ scope.row.message.type.text }}
                  </el-tag>
                  <el-tag type="danger" v-else>
                    {{ scope.row.message.type.text }}
                  </el-tag>
                  <span class="message-title">
                    {{ scope.row.message.title }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="date" width="150"></el-table-column>
              <el-table-column width="120">
                <template v-slot="scope">
                  <el-button type="danger" @click="deleteHandle(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="admin_table_main_pagination">
              <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :page-size="pageSize"
                :total="totalPage"
                :current-page="pageIndex"
                background
                layout="prev, pager, next,jumper,total">
              </el-pagination>
            </div>
            <div class="handle-row">
              <el-button type="danger" @click="deleteHandle()">删除全部</el-button>
            </div>
          </template>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup name="menu-category-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const dataForm = reactive([])
  const model = ref('user/message')
  const message = ref('first')
  const refresh = inject("refresh")
  const unread = ref([])
  const readed = ref([])

  // 获取数据列表
  const getDataList = () => {
    dataListLoading.value = true
    $http({
      url: $http.adornUrl('/user/message/list'),
      method: 'post',
      params: $http.adornParams({
        page: pageIndex,
        limit: pageSize,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        unread.value = data.data.unread
        readed.value = data.data.readed.data
        totalPage.value = data.data.readed.total
      } else {
        unread.value = []
        readed.value = []
        totalPage.value = 0
      }
      dataListLoading.value = false
    })
  }


  const handleRead = (id) => {
    $http({
      url: $http.adornUrl('/user/message/readed'),
      method: 'post',
      data: $http.adornData({
        id: id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        refresh()
      } else {
        $message.error(data.message)
      }
    })
  }

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    getDataList(model)
  })
</script>

<style lang="scss" scoped>
.message-title {
  cursor: pointer;
  margin-left: 10px;
}
.handle-row {
  margin-top: 30px;
}
</style>
